App.require(["yDate"], function (yDate) {
    var app = App.getCurrent();

    app.definePopUp("strpcdatePicker", {
        sureHandler: function (ev) {
            var minDate = this.data.minDate, maxDate = this.data.maxDate, yDate = this.data.yDate, time = this.data.time;
            if (maxDate && maxDate.compare(yDate.year, yDate.month, yDate.day) === -1) {
                yDate.year = maxDate.year;
                yDate.month = maxDate.month;
                yDate.day = maxDate.day;
                if (time && (time.hour > this.data.maxTime.hour || (time.hour == this.data.maxTime.hour && time.minute > this.data.maxTime.minute))) {
                    time.hour = this.data.maxTime.hour;
                    time.minute = this.data.maxTime.minute;
                }
            }
            if (minDate && minDate.compare(yDate.year, yDate.month, yDate.day) === 1) {
                yDate.year = minDate.year;
                yDate.month = minDate.month;
                yDate.day = minDate.day;
                if (time && (time.hour < this.data.minTime.hour || (time.hour == this.data.minTime.hour && time.minute < this.data.minTime.minute))) {
                    time.hour = this.data.minTime.hour;
                    time.minute = this.data.minTime.minute;
                }
            }
            var target = this.data.target || app.currentPage;
            if (time) {
                target.dispatchEvent({ type: "datetimepickersure", mark: this.data.mark, time: time, date: this.data.yDate, target: this, data: this.data });
            }
            else {
                target.dispatchEvent({ type: "datepickersure", mark: this.data.mark, date: this.data.yDate, target: this, data: this.data });
            }
            this.hidden();
        },
        cancelHandler: function (ev) {
            var target = this.data.target || app.currentPage;
            target.dispatchEvent({ type: "datepickercancel", mark: this.data.mark, date: this.data.yDate, target: this, data: this.data });
            this.hidden();
        },
        clearHandler: function (ev) {
            var target = this.data.target || app.currentPage;
            if (this.data.time) {
                target.dispatchEvent({ type: "datetimepickerclear", mark: this.data.mark, date: this.data.yDate, time: this.data.time, target: this, data: this.data });
            }
            else {
                target.dispatchEvent({ type: "datepickerclear", mark: this.data.mark, date: this.data.yDate, target: this, data: this.data });
            }
            this.hidden();
        },
        setPosition: function () {
            var bindDom = this.data.bindDom;
            if (bindDom) {
                var rect = bindDom.getBoundingClientRect();
                var popDiv = this.popDiv;
                popDiv.style.left = rect.left + "px";
                if (window.innerHeight > rect.bottom + 280) {
                    popDiv.style.top = (rect.bottom + 5) + "px";
                }
                else {
                    popDiv.style.bottom = (window.innerHeight - rect.top + 5) + "px";
                }
            }
        },
        hiddenByOutEnv: function () {
            this.hidden();
            var target = this.data.target || app.currentPage;
            target.dispatchEvent("datepickhiddenfromoutenv");
        },
        beforeShow: function () {
            if (document.activeElement) document.activeElement.blur();
            var data = this.data;
            if (!(data.year || data.month || data.day)) {
                var now = new Date();
                data.year = now.getFullYear();
                data.month = now.getMonth() + 1;
                data.day = now.getDate();
            }
            if (data.hour || data.minute) {
                data.time = {
                    hour: data.hour,
                    minute: data.minute
                }
            }
            data.yDate = new yDate(data.year, data.month, data.day);
            if (data.min) {
                data.minDate = new yDate(data.min.year, data.min.month, data.min.day);
                if (data.time) {
                    data.minTime = {
                        hour: data.min.hour,
                        minute: data.min.minute
                    }
                }
            }
            if (data.max) {
                data.maxDate = new yDate(data.max.year, data.max.month, data.max.day);
                if (data.time) {
                    data.maxTime = {
                        hour: data.max.hour,
                        minute: data.max.minute
                    }
                }
            }
            data.selectDate = {
                year: data.yDate.year,
                month: data.yDate.month,
                day: data.yDate.day
            };
            this.setPosition();
            this.attachDiyEvent("sure", this.sureHandler);
            this.attachDiyEvent("cancel", this.cancelHandler);
            this.attachDiyEvent("clear", this.clearHandler);
            return true;
        },
    })
});